import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired
    private LoginService loginService;

    @Role(value = Role.GUEST)
    @PostMapping("/api/login")
    public ResponseEntity<Void> login(@RequestParam String username, @RequestParam String password, HttpSession session) {
        RoleDao roleDao = new RoleDao();
        roleDao.setUsername(username);
        roleDao.setPassword(password);
        return loginService.login(roleDao,session);
    }

    @Role(value = Role.USER)
    @GetMapping("/api/user")
    public ResponseEntity<Void> getUser(@RequestParam String id,HttpSession session) {
        RoleDao roleDao = (RoleDao) session.getAttribute("user");
        if(roleDao.getRole() == Role.USER){
            //获取用户的接口
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.ok().build();

    }

    @Role(value = Role.ADMIN)
    @DeleteMapping("/api/user")
    public ResponseEntity<Void> deleteUser(@RequestParam String id,HttpSession session) {
        RoleDao roleDao = (RoleDao) session.getAttribute("user");
        if(roleDao.getRole() == Role.ADMIN){
            //删除的接口
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.ok().build();
    }
}
